package com.sinog.biz.service.api.impl;

import com.alibaba.fastjson.JSON;
import com.sinog.biz.config.RedisConfig;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.service.api.DataToRedisService;
import com.sinog.core.domain.SystemCode;
import com.sinog.core.model.SystemCodeVo;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc redis缓存字典表数据
 * @author -
 * @date -
 */
@Slf4j
@Service
public class DataToRedisServiceImpl implements DataToRedisService {

    @Resource
    private SystemCodeMapper systemCodeMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisConfig redisConfig;

    /**
     * 缓存中华人民共和国行政区划代码
     * @return Result
     */
    @Override
    public Result saveAreaCodeData() {
        log.info(">>>>>>>>>>开始同步areaCode:redis<<<<<<<<<<");
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询系统中tbsys_code表全部code_id和code_name
        List<Map<String,Object>> lists = systemCodeMapper.getCodeIdAndName(Constants.CodeType.CODE_ZHRMGHG_XZQH_DM);
        Map<String, String> maps = new HashMap<>(16);
        for(Map<String,Object> stringObjectMap : lists) {
            maps.put(stringObjectMap.get("codeId").toString(),stringObjectMap.get("codeName").toString());
        }
        stringRedisTemplate.opsForHash().putAll("areaCode",maps);
        log.info(">>>>>>>>>>结束同步areaCode:redis<<<<<<<<<<");
        return Result.success("保存成功");
    }

    /**
     * 罪名编码critypeCode、职业技能jineng
     * 罪名编码：critypeCode 2_12
     * 职业技能：jineng  4_1
     * @param keyName redis存储键
     * @param codeType 字典表tbsys_code的code_type
     * @return Result
     */
    @Override
    public Result saveDataToRedisCritype(String keyName,String codeType) {
        log.info(">>>>>>>>>>开始同步{}:redis<<<<<<<<<<",keyName);
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询系统中tbsys_code表全部code和name
        List<Map<String,Object>> lists = systemCodeMapper.getCodeIdAndName(codeType);
        Map<String, String> maps = new HashMap<>(16);
        for(Map<String,Object> stringObjectMap : lists) {
            maps.put(stringObjectMap.get("codeId").toString(),stringObjectMap.get("codeName").toString());
        }
        stringRedisTemplate.opsForHash().putAll(keyName,maps);
        log.info(">>>>>>>>>>结束同步{}:redis<<<<<<<<<<",keyName);
        return Result.success("保存成功");
    }

    /**
     * 罪名编码中文critypeZn
     * 罪名编码：critypeZn 2_12
     * @param keyName redis存储键
     * @param codeType 字典表tbsys_code的code_type
     * @return Result
     */
    @Override
    public Result saveDataToRedisCritypeZn(String keyName,String codeType) {
        log.info(">>>>>>>>>>开始同步{}:redis<<<<<<<<<<",keyName);
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询系统中tbsys_code表全部code和name
        List<Map<String,Object>> lists = systemCodeMapper.getCodeIdAndName(codeType);
        Map<String, String> maps = new HashMap<>(16);
        for(Map<String,Object> stringObjectMap : lists) {
            maps.put(stringObjectMap.get("codeName").toString(),stringObjectMap.get("codeId").toString());
        }
        stringRedisTemplate.opsForHash().putAll(keyName,maps);
        log.info(">>>>>>>>>>结束同步{}:redis<<<<<<<<<<",keyName);
        return Result.success("保存成功");
    }

    /**
     * 罪名分类编码fzlx
     * select code_id codeId,code_name codeName from tbsys_code where code_type=#{type} and pcode_id='-1'
     * 罪名分类编码：keyName：fzlx ，code_type：2_12
     * @param keyName redis存储键
     * @param codeType 字典表tbsys_code的code_type
     * @return Result
     */
    @Override
    public Result saveDataToRedisCritypeP(String keyName,String codeType) {
        log.info(">>>>>>>>>>开始同步{}:redis<<<<<<<<<<",keyName);
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询系统中tbsys_code表全部code和name
        List<Map<String,Object>> lists = systemCodeMapper.getCritypeCodeP(codeType);
        Map<String, String> maps = new HashMap<>(16);
        for(Map<String,Object> stringObjectMap : lists) {
            maps.put(stringObjectMap.get("codeId").toString(),stringObjectMap.get("codeName").toString());
        }
        stringRedisTemplate.opsForHash().putAll(keyName,maps);
        log.info(">>>>>>>>>>结束同步{}:redis<<<<<<<<<<",keyName);
        return Result.success("保存成功");
    }

    /**
     * 全国行政区划编码及父编码
     * @return Result
     */
    @Override
    public Result saveDataToRedisCodeParent() {
        log.info(">>>>>>>>>>开始同步{}:redis<<<<<<<<<<","areaCodeAndPcode");
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询系统中tbsys_code表全部code_id和code_name
        List<Map<String,Object>> lists = systemCodeMapper.getAreaCodeAndPcode();
        Map<String, String> maps = new HashMap<>(16);
        for(Map<String,Object> stringObjectMap : lists) {
            maps.put(stringObjectMap.get("codeId").toString(),stringObjectMap.get("pCode").toString());
        }
        stringRedisTemplate.opsForHash().putAll("areaCodeAndPcode",maps);
        log.info(">>>>>>>>>>结束同步{}:redis<<<<<<<<<<","areaCodeAndPcode");
        return Result.success("保存成功");
    }

    /**
     * 缓存捕前职业bqzy 2_8 key中文value code
     * @param keyName keyName
     * @return Result
     */
    @Override
    public Result saveDataToRedisBqzyZn(String keyName) {
        log.info(">>>>>>>>>>开始同步bqzyZn:redis<<<<<<<<<<");
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询系统中tbsys_code表全部code_id和code_name
        List<Map<String,Object>> lists = systemCodeMapper.getCodeIdAndName(Constants.CodeType.CODE_ZY_DM);
        Map<String, String> maps = new HashMap<>(16);
        for(Map<String,Object> stringObjectMap : lists) {
            maps.put(stringObjectMap.get("codeName").toString(),stringObjectMap.get("codeId").toString());
        }
        stringRedisTemplate.opsForHash().putAll("bqzyZn",maps);
        log.info(">>>>>>>>>>结束同步bqzyZn:redis<<<<<<<<<<");
        return Result.success("保存成功");
    }

    /**
     * 缓存捕前职业bqzy 2_8 key code value中文
     * @param keyName keyName
     * @return Result
     */
    @Override
    public Result saveDataToRedisBqzy(String keyName) {
        log.info(">>>>>>>>>>开始同步{}:redis<<<<<<<<<<",keyName);
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询系统中tbsys_code表全部code_id和code_name查询捕前职业
        List<Map<String,Object>> lists = systemCodeMapper.getCodeIdAndName(Constants.CodeType.CODE_ZY_DM);
        Map<String, String> maps = new HashMap<>(16);
        for(Map<String,Object> stringObjectMap : lists) {
            maps.put(stringObjectMap.get("codeId").toString(),stringObjectMap.get("codeName").toString());
        }
        stringRedisTemplate.opsForHash().putAll("bqzy",maps);
        log.info(">>>>>>>>>>结束同步{}:redis<<<<<<<<<<",keyName);
        return Result.success("保存成功");
    }

    /**
     * 缓存罪名对应的顶级犯罪类型
     * @param keyName keyName
     * @return Result
     */
    @Override
    public Result saveDataToRedisCritypeAndFzlx(String keyName) {
        log.info(">>>>>>>>>>开始同步{}:redis<<<<<<<<<<",keyName);
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        //查询所有罪名ID
        List<String> codeIds = systemCodeMapper.getCodeIds();
        Map<String,String> codeAndPcode = new HashMap<>(16);
        for(String codeId : codeIds) {
            String pcodeTemp = "";
            //查询父ID
            SystemCode systemCode = systemCodeMapper.getByCode(Constants.CodeType.CODE_ZMFL_DM, codeId);
            String pcodeId = systemCode.getPcodeId();
            while(!Constants.NumberStr.NUM_STR_FU_ONE.equals(pcodeId)) {
                pcodeTemp = pcodeId;
                pcodeId = systemCodeMapper.getByCode(Constants.CodeType.CODE_ZMFL_DM,pcodeId).getPcodeId();
            }
            codeAndPcode.put(codeId,pcodeTemp);
        }
        stringRedisTemplate.opsForHash().putAll(keyName,codeAndPcode);
        log.info(">>>>>>>>>>结束同步{}:redis<<<<<<<<<<",keyName);
        return Result.success("保存成功");
    }

    /**
     * 描述
     * @param keyName keyName
     * @return List<String>
     */
    @Override
    public List<String> selectAreaStr(String keyName) {
        List<String> list = JSON.parseArray(keyName,String.class);
        return systemCodeMapper.selectAreaStr(list);
    }

    /**
     * 描述
     * @param pushTbsysCode pushTbsysCode
     * @return Result
     */
    @Override
    public Result saveDataToRedisXzqh(String pushTbsysCode) {
        log.info(">>>>>>>>>>开始同步redis<<<<<<<<<<");
        JedisPool jedisPool = redisConfig.redisPoolFactory();
        Jedis jedis = jedisPool.getResource();
        // 初始化全省。
        List<String> localCodes = new ArrayList<>();
        localCodes.add("110000");
        localCodes.add("120000");
        localCodes.add("130000");
        localCodes.add("140000");
        localCodes.add("150000");
        localCodes.add("210000");
        localCodes.add("220000");
        localCodes.add("230000");
        localCodes.add("310000");
        localCodes.add("320000");
        localCodes.add("330000");
        localCodes.add("340000");
        localCodes.add("350000");
        localCodes.add("360000");
        localCodes.add("370000");
        localCodes.add("410000");
        localCodes.add("420000");
        localCodes.add("430000");
        localCodes.add("440000");
        localCodes.add("450000");
        localCodes.add("460000");
        localCodes.add("500000");
        localCodes.add("510000");
        localCodes.add("520000");
        localCodes.add("530000");
        localCodes.add("540000");
        localCodes.add("610000");
        localCodes.add("620000");
        localCodes.add("630000");
        localCodes.add("640000");
        localCodes.add("650000");
        localCodes.add("660000");
        localCodes.add("710000");
        localCodes.add("800000");
        localCodes.add("810000");
        try {
            for(String localCode : localCodes) {
                //查询系统中tbsys_code表全部记录
                List<SystemCodeVo> systemCodes = systemCodeMapper.searchByCode(localCode.substring(0,2));
                String systemCodesToString = JsonUtil.toText(systemCodes);
                jedis.hset("pushTbsysCode",localCode,systemCodesToString);
            }
        } catch (Exception e) {
            log.error("执行异常:{}",e.getMessage());
        } finally {
            jedis.close();
        }
        log.info(">>>>>>>>>>结束同步redis<<<<<<<<<<");
        return Result.success("保存成功");
    }

    /**
     * 全省编码加名称
     * @param keyName keyName
     * @return Result
     */
    @Override
    public Result saveDataToRedisProvince(String keyName) {
        log.info(">>>>>>>>>>开始同步{}:redis<<<<<<<<<<",keyName);
        //拿到所有的省份
        List<Map<String,String>> getProvinceAll = systemCodeMapper.getProvinceAll();
        getProvinceAll.stream().count();
        //存储省份的map
        for(Map<String,String> item : getProvinceAll) {
            stringRedisTemplate.opsForHash().put(keyName,item.get("codeId"),item.get("codeName"));
        }
        log.info(">>>>>>>>>>结束同步{}:redis<<<<<<<<<<",keyName);
        return Result.success("保存成功");
    }
}